package com.sc.nft.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.sc.nft.admin.request.*;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.annotation.UserLogin;
import com.sc.nft.dao.DaoEcologyPointsDetailDao;
import com.sc.nft.dao.DaoGasDetailDao;
import com.sc.nft.dao.DaoWalletDao;
import com.sc.nft.dao.DaoWalletDetailDao;
import com.sc.nft.entity.DaoWalletDetail;
import com.sc.nft.entity.UserCreatorInfo;
import com.sc.nft.entity.dto.*;
import com.sc.nft.entity.vo.*;
import com.sc.nft.entity.vo.salvageEvents.PageDaoEcologyPointsTransferRecordsVO;
import com.sc.nft.enums.RedisKeyEnum;
import com.sc.nft.enums.WalletChannelEnum;
import com.sc.nft.enums.wallet.DaoTransferTypeEnum;
import com.sc.nft.enums.wallet.DaoWalletTypeEnum;
import com.sc.nft.service.DaoTransferService;
import com.sc.nft.service.DigitalCollectionService;
import com.sc.nft.service.UserCreatorInfoService;
import com.sc.nft.sup.EnumVO;
import com.sc.nft.sup.ErrorCode;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/userCreatorInfo")
@Api(tags = "用户dao")
public class UserCreatorInfoAdminController {

    private final UserCreatorInfoService userCreatorInfoService;

    private final DigitalCollectionService digitalCollectionService;
    private final DaoGasDetailDao daoGasDetailDao;
    private final DaoWalletDao daoWalletDao;
    private final DaoWalletDetailDao daoWalletDetailDao;
    private final DaoEcologyPointsDetailDao daoEcologyPointsDetailDao;
    private final DaoTransferService daoTransferService;
    private final Redisson redisson;

    @ApiOperation(value = "获取dao列表", notes = "获取dao列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getPageList", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<AdminUserCreatorInfoVO> getPageList(@RequestBody AdminPageListDTO adminPageListDTO) {
        Page<AdminUserCreatorInfoVO> adminPageList = userCreatorInfoService.getAdminPageList(adminPageListDTO);
        return PageResult.success(adminPageList);
    }


    @ApiOperation(value = "新增品牌", notes = "新增品牌", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/insert", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result insert(@Valid @RequestBody AddBrandVO addBrandVO) {
        userCreatorInfoService.adminInsert(addBrandVO);
        return Result.success();
    }

    @ApiOperation(value = "创建dao", notes = "创建dao", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/creationDao", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result creationDao(@Valid @RequestBody CreationDaoDTO creationDaoDTO) {
        userCreatorInfoService.creationDao(creationDaoDTO);
        return Result.success();
    }

    @ApiOperation(value = "查询dao信息", notes = "查询dao信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/findByDaoId", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<CreationDaoDTO> creationDao(@Valid @RequestBody IdRequest request) {
        return Result.success(userCreatorInfoService.findByDaoId(request.getId()));
    }


    @ApiOperation(value = "修改dao", notes = "创建dao", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/updateDao", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result updateDao(@Valid @RequestBody CreationDaoDTO creationDaoDTO) {
        userCreatorInfoService.updateDao(creationDaoDTO);
        return Result.success();
    }

    @ApiOperation(value = "DaoGAS充值", notes = "DaoGAS充值", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/gasReg", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result gasReg(@RequestBody GasRegDTO dto) {
        UserCreatorInfo userCreatorInfo = userCreatorInfoService.getById(dto.getId());
        if (userCreatorInfo == null) {
            return Result.fail("Dao不存在");
        }
        if (!userCreatorInfo.getStatus().equals(1)) {
            return Result.fail("该组织不是dao");
        }
        userCreatorInfoService.gasReg(dto);
        return Result.success();
    }

    @PostMapping(value = "/getGasDetail", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取gas信息明细", notes = "获取gas信息明细", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public PageResult<DaoGasDetailVO> getGasDetail(@RequestBody TypeDTO request) {
        return PageResult.success(daoGasDetailDao.getPage(request.getDaoId(), request.getType(), request.toMp()), DaoGasDetailVO.class);
    }


    @ApiOperation(value = "查询品牌自己发行的藏品", notes = "查询品牌自己发行的藏品", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getCollectionList", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result getCollectionList(@RequestBody IdRequest idRequest) {
        List<GetAdminCollectionVO> collectionList = digitalCollectionService.getCollectionList(idRequest.getId());
        return Result.success(collectionList);
    }

    @ApiOperation(value = "修改品牌", notes = "查询品牌自己发行的藏品", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/updateBrand", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result updateBrand(@RequestBody UpdateBrandDTO updateBrandDTO) {
        userCreatorInfoService.updateBrand(updateBrandDTO);
        return Result.success();
    }

    @ApiOperation(value = "根据id获取品牌", notes = "根据id获取品牌", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getById", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<AdminGetByIdVO> getById(@RequestBody IdRequest idRequest) {
        AdminGetByIdVO adminGetByIdVO = userCreatorInfoService.adminGetById(idRequest.getId());
        return Result.success(adminGetByIdVO);
    }

    @ApiOperation(value = "获取所有的dao", notes = "获取所有的dao", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getAll", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result getAll() {
        List<AdminUserCreatorInfoAllDao> all = userCreatorInfoService.getAll();
        return Result.success(all);
    }

    @ApiOperation(value = "获取所有的dao", notes = "获取所有的dao", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getAllByStatus", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result getAllByStatus() {
        return Result.success(userCreatorInfoService.getAllByStatus());
    }


    @ApiOperation(value = "条件查询企业账号转账记录", notes = "条件查询企业账号转账记录", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getPageDaoEcologyPointsTransferRecords", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<PageDaoEcologyPointsTransferRecordsVO> getPageDaoEcologyPointsTransferRecords(@Valid @RequestBody PageDaoEcologyPointsTransferRecordsRequest request) {
        return PageResult.success(userCreatorInfoService.getPageDaoEcologyPointsTransferRecords(request.getDaoId(), request.getToUserTel(), request.getStartTime(), request.getEndTime(), request.getPageNo(), request.getPageSize()));
    }

    @ApiOperation(value = "品牌钱包列表", notes = "品牌钱包列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/walletPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<DaoWalletVO> walletPage(@RequestBody @Valid NamePageRequest request) {
        return PageResult.success(daoWalletDao.getAdminPage(request.getName(), request.toMp()));
    }

    @ApiOperation(value = "品牌钱包列表", notes = "品牌钱包列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/walletDetail", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<DaoWalletVO> walletDetail(@RequestBody @Valid IdRequest request) {
        return Result.success(daoWalletDao.getAdminDetail(request.getId()));
    }

    @ApiOperation(value = "品牌钱包燃料明细", notes = "品牌钱包燃料明细", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/ecologyPointsDetailPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<DaoEcologyPointsDetailVO> ecologyPointsDetailPage(@RequestBody @Valid EcologyPointsDetailPageRequest request) {
        return PageResult.success(daoEcologyPointsDetailDao.ecologyPointsDetailPage(request.getId(), request.getAmountType(), request.getType(), request.toMp()));
    }

    @ApiOperation(value = "品牌钱包明细", notes = "品牌钱包明细", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/walletDetailPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<DaoWalletDetailVO> walletDetailPage(@RequestBody @Valid DaoWalletRecordRequest request) {
        Page<DaoWalletDetail> page = daoWalletDetailDao.getByDaoIdAndType(request.getId(), request.getType(), request.toMp());
        Page<DaoWalletDetailVO> voPage = new Page<>(page.getCurrent(), page.getSize(), page.getTotal());
        List<DaoWalletDetailVO> result = Lists.newArrayList();
        for (DaoWalletDetail record : page.getRecords()) {
            DaoWalletDetailVO daoWalletDetailVO = new DaoWalletDetailVO();
            daoWalletDetailVO.setAmount(record.getAmount());
            daoWalletDetailVO.setCharge(record.getCharge());
            daoWalletDetailVO.setCreateTime(record.getCreateTime());
            daoWalletDetailVO.setRealAmount(record.getRealAmount());
            daoWalletDetailVO.setBeforeAmount(record.getBeforeAmount());
            daoWalletDetailVO.setIsAdd(record.getIsAdd());
            daoWalletDetailVO.setAfterAmount(record.getAfterAmount());
            daoWalletDetailVO.setType(record.getType());
            daoWalletDetailVO.setTypeTxt(record.getType().getDescription());
            daoWalletDetailVO.setRemark(record.getRemark());
            result.add(daoWalletDetailVO);
        }
        voPage.setRecords(result);
        return PageResult.success(voPage);
    }


    @ApiOperation(value = "审批", notes = "审批", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/approval", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result approval(@RequestBody @Valid ApprovalRequest request) {
        RLock lock = redisson.getLock(RedisKeyEnum.DAO_WALLET_APPROVAL + request.getId().toString());
        try {
            boolean res = lock.tryLock(1, TimeUnit.SECONDS);
            if (res) {
                daoTransferService.approval(request.getId(), request.getPass(), request.getRemark());
            } else {
                return Result.fail(ErrorCode.LATER_TRY);
            }
        } catch (InterruptedException e) {
            return Result.fail("操作人数过多,请稍后再试");
        } finally {
            lock.unlock();
        }
        return Result.success();
    }

    @ApiOperation(value = "批量审批", notes = "批量审批", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/batchApproval", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result batchApproval(@RequestBody @Valid ApprovalBatchRequest request) {
        for (Long id : request.getIds()) {
            RLock lock = redisson.getLock(RedisKeyEnum.DAO_WALLET_APPROVAL + id.toString());
            try {
                boolean res = lock.tryLock(1, TimeUnit.SECONDS);
                if (res) {
                    daoTransferService.approval(id, request.getPass(), request.getRemark());
                } else {
                    return Result.fail(ErrorCode.LATER_TRY);
                }
            } catch (InterruptedException e) {
                return Result.fail("操作人数过多,请稍后再试");
            } finally {
                lock.unlock();
            }
        }

        return Result.success();
    }


    @ApiOperation(value = "企业转账记录", notes = "企业转账记录", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/transferRecord", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<DaoTransferDetailVO> transferRecord(@RequestBody @Valid DaoTransferRecordRequest request) {
        return PageResult.success(daoTransferService.getRecords(request.getDaoId(), 1, request.getType(), request.getApplyTel(), request.getMarketAccount(), request.getWalletChannel(), request.getStatus(), request.getStartTime(), request.getEndTime(), request.toMp()));
    }

    @ApiOperation(value = "市值转账记录", notes = "市值转账记录", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/marketTransferRecord", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<DaoTransferDetailVO> marketTransferRecord(@RequestBody @Valid MarketTransferRecordRequest request) {
        return PageResult.success(daoTransferService.getRecords(request.getDaoId(), 2, null, null, request.getMarketAccount(), null, request.getStatus(), request.getStartTime(), request.getEndTime(), request.toMp()));
    }

    @ApiOperation(value = "转账记录类型列表", notes = "转账记录类型列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/transferTypeList", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<List<EnumVO>> transferTypeList() {
        List<EnumVO> result = Lists.newArrayList();
        for (DaoTransferTypeEnum value : DaoTransferTypeEnum.values()) {
            result.add(new EnumVO(value.name(), value.getCode(), value.getDescription()));
        }
        return PageResult.success(result);
    }

    @ApiOperation(value = "钱包渠道列表", notes = "钱包渠道列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/walletChannelList", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<List<EnumVO>> walletChannelList() {
        List<EnumVO> result = Lists.newArrayList();
        for (WalletChannelEnum value : WalletChannelEnum.values()) {
            result.add(new EnumVO(value.name(), value.getCode(), value.getDescription()));
        }
        return PageResult.success(result);
    }


    @ApiOperation(value = "查询操作类型", notes = "查询操作类型", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/walletTypeList", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<List<EnumVO>> walletTypeList() {
        List<EnumVO> list = Lists.newArrayList();
        for (DaoWalletTypeEnum value : DaoWalletTypeEnum.values()) {
            list.add(new EnumVO(value.name(), value.getCode(), value.getDescription()));

        }
        return Result.success(list);
    }

    @ApiOperation(value = "更新星级", notes = "更新星级", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/updateStarRating", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<Boolean> updateStarRating(@RequestBody @Valid DaoUpdateStarRatingRequest request) {
        return Result.success(userCreatorInfoService.updateStarRating(request.getId(), request.getStarScore()));
    }

    @ApiOperation(value = "更新是否开启生态池", notes = "更新是否开启生态池", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/updateEnableEcologyPool", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<Boolean> updateEnableEcologyPool(@RequestBody @Valid DaoUpdateEnableEcologyPoolRequest request) {
        return Result.success(userCreatorInfoService.updateEnableEcologyPool(request.getId(), request.getEnableEcologyPool()));
    }

    @ApiOperation(value = "更新是否加入排行榜", notes = "更新否加入排行榜", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/updateEnableJoinRank", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<Boolean> updateEnableJoinRank(@RequestBody @Valid DaoUpdateEnableJoinRankRequest request) {
        return Result.success(userCreatorInfoService.updateEnableJoinRank(request.getId(), request.getEnableJoinRank()));
    }

    @ApiOperation(value = "获取dao手机号列表", notes = "获取dao手机号列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getPhonePage", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<AdminUserCreatorInfoPhoneVO> getPageList(@RequestBody AdminPhonePageDTO adminPhonePageDTO) {
        Page<AdminUserCreatorInfoPhoneVO> userCreatorPhonePage = userCreatorInfoService.findUserCreatorPhoneList(adminPhonePageDTO);
        return PageResult.success(userCreatorPhonePage);
    }


    @ApiOperation(value = "新增dao手机号", notes = "新增dao手机号", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/userCreatorAddPhone", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result userCreatorAddPhone(@Valid @RequestBody UserCreatorPhoneAddDTO userCreatorPhoneAddDTO) {
        userCreatorInfoService.userCreatorAddPhone(userCreatorPhoneAddDTO);
        return Result.success();

    }


    /**
     * 删除dao中手机号  赋null
     *
     * @param idRequest
     * @return
     */
    @PostMapping(value = "/deleteUserCreatorPhoneById", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除", notes = "删除", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result deleteById(@RequestBody IdRequest idRequest) {
        userCreatorInfoService.updateDaoPhoneNoStatus(idRequest.getId());
        return Result.success();
    }

    @ApiOperation(value = "更新是否开启saas展示生态熔炼", notes = "更新是否开启saas展示生态熔炼", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/updateShowEcologyStatus", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result updateShowEcologyStatus(@RequestBody @Valid DaoUpdateShowEcologyStatusRequest request) {
        userCreatorInfoService.updateShowEcologyStatus(request.getId(), request.getShowEcologyStatus());
        return Result.success();
    }

    @ApiOperation(value = "更新是否展示销毁质押", notes = "更新是否展示销毁质押", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/updateShowPledgeStatus", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result updateShowPledgeStatus(@RequestBody @Valid DaoShowPledgeStatusRequest request) {
        userCreatorInfoService.updateShowPledgeStatus(request.getId(), request.getShowPledgeStatus());
        return Result.success();
    }

}
